package com.sxx.beautifulcity.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.adapter.RecommendSoftAdapter;
import com.sxx.beautifulcity.constant.Constant;
import com.sxx.beautifulcity.dialog.CommonProgressDialog;
import com.sxx.beautifulcity.entity.RecommendSoftData;
import com.sxx.beautifulcity.listenerInterface.OnDownLoadClickListener;
import com.sxx.beautifulcity.okhttp.OkHttpClientManager;
import com.sxx.beautifulcity.utils.VolleySingleton;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/11/10.
 * 推荐软件
 */
public class RecommendSoftActivity extends AppCompatActivity implements
        View.OnClickListener, OnDownLoadClickListener {
    private final static String TAG = "RecommendSoftActivity";
    private RecyclerView recyclerView;
    private RecommendSoftAdapter adapter;
    private TextView titleTv , moreTv;
    private ImageView backIv;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerview_recommend_soft);
        initView();
        requestSoft();

    }

    /***
     * 初始化
     */

    private void initView() {
        titleTv = (TextView) findViewById(R.id.titleTv);
        titleTv.setText("推荐软件");
        moreTv = (TextView) findViewById(R.id.moreTv);
        moreTv.setText("");
        backIv = (ImageView) findViewById(R.id.backIV);
        backIv.setOnClickListener(this);

        recyclerView = (RecyclerView) findViewById(R.id.recyvlerView);
        adapter = new RecommendSoftAdapter();
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        adapter.setOnDownLoadClickListener(this);

    }

    /***
     * 请求网络数据
     */
    private void requestSoft() {
        final Dialog dialog = CommonProgressDialog.createLoadingDialog(this , "正在加载中");
        dialog.show();
        String url = Constant.RECOMMENDSOF_PATH;
        JsonObjectRequest request = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            Gson gson = new Gson();
            List<RecommendSoftData> list = new ArrayList<>();

            @Override
            public void onResponse(JSONObject response) {
                if (response != null && response.has("data")) {
                    try {
                        JSONArray array = response.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++) {
                            String str = array.getString(i);
                            RecommendSoftData data = gson.fromJson(str , RecommendSoftData.class);
                            list.add(data);
                        }
                        adapter.addData(list);
                        dialog.dismiss();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return super.getHeaders();
            }
        };
        VolleySingleton.getInstance().addRequest(request, TAG);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backIV:
                Intent intent = new Intent();
                intent.setClass(this,MainActivity.class);
                startActivity(intent);
                finish();

                break;
        }
    }

    /***
     * 下载监听
     * @param progressBar 从适配器里面传过来的进度条
     * @param url  下载的网址
     */
    @Override
    public void onDownloadClick(ProgressBar progressBar, String url) {
        downLoad(url, progressBar);
    }
    /**
     * 下载软件
     * @param url 软件地址
     * @param progressBar 显示的进度条
     */
    private void downLoad(String url , final ProgressBar progressBar) {
        Log.i("taggg" , url);
//        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
//        Log.d("TAGGG",path);
        OkHttpClientManager.downloadAsyn(url, "sdcard/cache/", new OkHttpClientManager.ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Log.d("TAGGG", "ERROR" + e);
                    }

                    @Override
                    public void onResponse(String response) {
                        Log.d("TAGGG", "路径:--->" + response);
                    }

                    @Override
                    public void inprogress(float progress) {
                        progressBar.setProgress((int) (100 * progress));
                    }
                }
        );
    }




}
